package org.example;

// 建议 OpenAI Java SDK版本 >= 3.5.0
//  Maven依赖
//  <dependency>
//      <groupId>com.openai</groupId>
//      <artifactId>openai-java</artifactId>
//      <version>3.5.0</version>
//  </dependency>
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import org.example.utils.COMMON;

import java.util.Arrays;


public class SingleConversation {
    public static void main(String[] args) {
        try {
            Message userMsg = Message.builder()
                    .role(Role.USER.getValue())
                    .content("你是谁")
                    .build();
            Message assistantMsg = Message.builder()
                    .role(Role.SYSTEM.getValue())
                    .content("you are a helpful assistant.")
                    .build();

            GenerationParam param = GenerationParam.builder()
                    .apiKey(COMMON.QINIUYUN_AK)
                    .model(COMMON.MODEL)
                    .messages(Arrays.asList(assistantMsg, userMsg))
                    .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                    .build();
            Generation gen = new Generation();
            GenerationResult chatCompletion = gen.call(param);
            String answer = chatCompletion.getOutput().getChoices().get(0).getMessage().getContent();
            // 若需要以Json格式输出，可使用fastjson2.JSON中的JSON.toJSONString(chatCompletion)
            System.out.println(answer);

        } catch (Exception e) {
            System.err.println("错误信息：" + e.getMessage());
            System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        }
    }
}